package it.polimi.entities.web;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import it.polimi.entitiesDTO.FlightDTO;
import it.polimi.entitiesManagers.TransportMgr;

@ManagedBean(name = "FlightBean")
@SessionScoped
public class FlightBean {

	@EJB
	private TransportMgr tmgr;
	private FlightDTO flightDTO;
	private FlightDTO flightDTO2;
	private List<String> ff;
	private List<String> ff2;	
	private List<String> ff3;	
	private String name;	
	private String name2;	
	private String name3;
	private int id;
	private List<Integer> allId;
	
	/* -----------------------------
	 * ---------class methods-------
	 * -----------------------------
	 */
	
	private void clearAll(){
		this.flightDTO = new FlightDTO();
		this.flightDTO2= new FlightDTO();
		this.ff = new ArrayList<String>();		
		this.ff2 = new ArrayList<String>();		
		this.ff3 = new ArrayList<String>();		
		this.name = new String();		
		this.name2= new String();		
		this.name3 = new String();
		this.id=0;
		this.setAllId(new ArrayList<Integer>());
	}
	
	private void fillFlight(){
		allId=tmgr.findAllId();
	}
	public void searchByArrival(){
		ff = tmgr.findFlightByArrival(name);
	}
	
	public void searchByDeparture(){
		ff2 = tmgr.findFlightByDeparture(name2);
	}

	public void searchByAirport(){
		ff3 = tmgr.findFlightByAirport(name3);
	}
	
	public FlightBean() {
		clearAll();
	}

	public String register() {
		tmgr.save(this.flightDTO);
		clearAll();
		return "index?faces-redirect=true";
	}
	
	public String updateFlight(){
		fillFlight();
		if(allId.contains(id)){
			this.flightDTO2.setId(id);
			tmgr.update(flightDTO2);
			clearAll();
			return "index?faces-redirect=true";
			}
			else
			return "/user/errorPage";
	}
	
	public String remove(){
		this.allId = tmgr.findAllId();
		if(this.allId.contains(this.id))
			{tmgr.remove(this.id);
			clearAll();
			return "/admin/index";
			}
		else return "/admin/errorPage";
	}

	/* -----------------------------
	 * -------SETTERS & GETTERS-----
	 * -----------------------------
	 */
	
	public FlightDTO getFlightDTO() {
		return flightDTO;
	}

	public void setFlightDTO(FlightDTO flightDTO) {
		this.flightDTO = flightDTO;
	}
	
	public List<String> getFf() {
		return ff;
	}
	
	public void setFf(List<String> ff) {
		this.ff = ff;
	}
	
	public String getName2() {
		return name2;
	}
	
	public void setName2(String name2) {
		this.name2 = name2;
	}
	
	public List<String> getFf2() {
		return ff2;
	}
	
	public void setFf2(List<String> ff2) {
		this.ff2 = ff2;
	}
	
	public List<String> getFf3() {
		return ff3;
	}
	
	public void setFf3(List<String> ff3) {
		this.ff3 = ff3;
	}
	
	public String getName3() {
		return name3;
	}
	
	public void setName3(String name3) {
		this.name3 = name3;
	}
	
	public void setName(String name){
		this.name=name;
	}
	
	public String getName(){
		return this.name;
	}


	public FlightDTO getFlightDTO2() {
		return flightDTO2;
	}


	public void setFlightDTO2(FlightDTO flightDTO2) {
		this.flightDTO2 = flightDTO2;
	}


	public int getId() {
		return id;
	}


	public void setId(int id) {
		this.id = id;
	}

	public List<Integer> getAllId() {
		return allId;
	}

	public void setAllId(List<Integer> allId) {
		this.allId = allId;
	}
	

}
